import {login} from '@/api/user'
import {setToken,setUserName,setUserId} from '@/libs/util.js'

const Login1 = {
  state: {
    // 共享的数据
    token: "",
    username: "",
    user_id: "",
  },
  getters:{
    username:state => state.username
  },
  mutations: {
    // 
    // setToken自定义名字,state参数1,token参数2是用户传递过来的
    set_Token (state,token){
      state.token = token
    },
    set_UserName (state,username){
      state.username = username
    },
    set_UserId (state,userid){
      state.user_id = userid
    }
  },
  actions:{
    // 
    Login(content, requestData){
      return new Promise((resolve, reject) => {
        login(requestData).then((res =>{
          let data = res.data.data
          // 保存token
          content.commit("set_Token", data.token);
          content.commit("set_UserName",data.username);
          content.commit("set_UserId",data.uid);
          setToken(data.token);
          setUserName(data.username);
          setUserId(data.uid)
          resolve(res)
        })).catch(err =>{
          console.log("fail")
          reject(err)
        })
      })
    }
  }
}

export default Login1;